<?php



namespace App\Zesow;

use App\Services\Base\Interfaces\ConfigServiceInterface;
use App\Services\Base\Services\ConfigService;
use App\Services\Work\Models\Company;
use EasyWeChat\Factory;

class Work
{
    private static $instance = null;

    private function __construct()
    {
    }

    /**
     * @return \EasyWeChat\Work\Application
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public static function getInstance($corpId = 0)
    {
        if (!self::$instance) {
            /**
             * @var ConfigService $configService
             */
            $configService = app()->make(ConfigServiceInterface::class);
            $config        = $configService->getWorkConfig();
            if (!empty($corpId)) {
                $workWechat = Company::query()->find($corpId);
                if (!empty($workWechat)) {
                    $config['corp_id']  = $workWechat->getAttribute('corp_id');
                    $config['secret']   = $workWechat->getAttribute('secret');
                    $config['agent_id'] = $workWechat->getAttribute('agent_id');
                }
            }
            self::$instance = Factory::work($config);
        }

        return self::$instance;
    }


}
